﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Role_ListController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Manager/Role_List.cshtml";
        protected string keywords = string.Empty;
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("manager_role", CMSEnums.ActionEnum.View.ToString()); //检查权限            
        }
        // GET: /admin/Role_List/

        public IActionResult Index()
        {
            this.keywords = HttpContext.Request.Query["keywords"].FirstOrDefault();
            ViewBag.Keywords = this.keywords;
            var model = GetAdminInfo(); //取得当前管理员信息
            RptBind("where role_type>=" + model.role_type + CombSqlTxt(this.keywords));
            return View(WEB_VIEW);
        }

        [HttpPost]
        public IActionResult SubmitDelete(string json)
        {
            ChkAdminLevel("manager_role", CMSEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            JObject jobject = JObject.Parse(json);
            JToken record = jobject["list"];
            int id;
            foreach (JToken item in record)
            {
                if (int.TryParse(item["id"].ToString(), out id))
                {
                    if (_manager_role.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(CMSEnums.ActionEnum.Delete.ToString(), "删除管理角色" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            string ClientScript = JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("index", "keywords={0}", this.keywords));
            return Content(ClientScript);
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere)
        {
            ViewData["list"] = _manager_role.GetList(_strWhere);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();            
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and role_name like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回角色类型名称=========================
        protected string GetTypeName(int role_type)
        {
            string str = "";
            switch (role_type)
            {
                case 1:
                    str = "超级用户";
                    break;
                default:
                    str = "系统用户";
                    break;
            }
            return str;
        }
        #endregion
    }
}
